<?php
include "include/config.inc.php";
error_reporting(E_ALL^ E_WARNING);

$drawArray = array();
$loanDrawIdHidden = 0;
$isReceivedCheck  = 'Y';

//Submit Card No and Display Loan Draw Detail
if(isset($_POST['submitBtn']) && $_POST['cardNo'] != "")
{
  $cardNo = $_POST['cardNo'];
  $drawDetail = "SELECT loandraw.loanDrawId, party.partyName, loandraw.drawInstallmentReceived, MAX( drawinstallment .drawInstallmentDate) AS lastInstallmentDate,
                        (loandraw.drawInstallmentAmount + loandraw.drawLoanInterest) AS installment
                   FROM loandraw
                   JOIN party ON party.partyId = loandraw.partyId
                   JOIN drawinstallment ON drawinstallment.loanDrawId = loandraw.loanDrawId
                  WHERE loandraw.loanDrawId =".$cardNo;
  $drawDetailRes = mysql_query($drawDetail);
  if($drawRow = mysql_fetch_array($drawDetailRes))
  {
  	$loanDrawIdHidden = $drawRow['loanDrawId'];
  	$drawArray['loanDrawId']              = $drawRow['loanDrawId'];
  	$drawArray['partyName']               = $drawRow['partyName'];
  	$drawArray['drawInstallmentReceived'] = $drawRow['drawInstallmentReceived'];
  	$drawArray['lastInstallmentDate']     = $drawRow['lastInstallmentDate'];
  	$drawArray['installment']             = $drawRow['installment'];
  }
  
  $drawCheckIsReceived = "SELECT loanDrawId,isReceived
                            FROM drawinstallment
                           WHERE loanDrawId =".$cardNo;
  $drawCheckIsReceivedRes = mysql_query($drawCheckIsReceived);
  while($drawCheckRow = mysql_fetch_array($drawCheckIsReceivedRes))
  {
  	$isReceivedCheck = $drawCheckRow['isReceived'];
  }
}

//Reciev Installment Process
if(isset($_POST['submit']) && isset($_POST['installment']))
{
  $loanDrawId = $_POST['loanDrawId'];
  $selectMinDate = "SELECT MIN(drawInstallmentDate) AS drawInstallmentDate
                      FROM drawinstallment
                     WHERE loanDrawId = '".$loanDrawId."'
                       AND isReceived = 'N'";
  $res = mysql_query($selectMinDate);
  if($dateRow=mysql_fetch_array($res))
  {
  	$loanDate = date('Y-m-d');
  	$startDate = $dateRow['drawInstallmentDate'];
    $loanInstallmentMonth = $_POST['installment'];
    $endDate=date('Y-m-d', strtotime($startDate. ' + '.$loanInstallmentMonth. 'month'));
    $nextDate = $startDate;
    if($startDate != NULL)
    {
      while(strtotime($nextDate) < strtotime($endDate))
      {
         //Update isReceived and receiveDate in drawinstallment table
        $updateDraw = "UPDATE drawinstallment 
                          SET isReceived  = 'Y', receiveDate = NOW()
                        WHERE loanDrawId  = ".$loanDrawId." 
                          AND drawInstallmentDate = '".$nextDate."'";
        $result = mysql_query($updateDraw) or die("Error Found..".mysql_error());
        $nextDate  = date ("Y-m-d", strtotime("+1 month", strtotime($nextDate)));
      }
    }
  }
  
  //update drawInstallmentReceived plus in loanDraw table
  $loanDraw = $_POST['installment'];
  $select = "SELECT SUM( drawInstallmentReceived + '$loanDraw' ) AS total
               FROM loandraw
              WHERE loanDrawId =".$loanDrawId;
  $resultDraw = mysql_query($select);
  if($rowRes = mysql_fetch_array($resultDraw))
  {
    $updateLoanDraw = "UPDATE loandraw 
                          SET drawInstallmentReceived = '".$rowRes['total']."'
                        WHERE loanDrawId = ".$loanDrawId;
    $loanMysql = mysql_query($updateLoanDraw);
  }
}
$smarty->assign('drawArray', $drawArray);
$smarty->assign('isReceivedCheck', $isReceivedCheck);
$smarty->assign('loanDrawIdHidden', $loanDrawIdHidden);
$smarty->display('drawLoansDetail.tpl');
?>